package com.innoking.docsys.doc.service.impl;

import com.innoking.docsys.borrow.module.MyBorrowVo;
import com.innoking.docsys.doc.dao.cluster.ClusterCmsBorRegisterDao;
import com.innoking.docsys.doc.dao.cluster.ClusterCmsBorRegisterExtDao;
import com.innoking.docsys.doc.dao.cluster.ClusterCmsBrowseRegisterDao;
import com.innoking.docsys.doc.dto.CmsBorRegister;
import com.innoking.docsys.doc.dto.CmsBrowseRegister;
import com.innoking.docsys.doc.dto.CmsSinItem;
import com.innoking.docsys.doc.example.CmsBorRegisterClusterExample;
import com.innoking.docsys.doc.service.MyBorrowService;
import com.innoking.docsys.utils.FastDfsClient;
import com.innoking.docsys.utils.ResultObjVO;
import com.innoking.docsys.utils.ResultUtil;
import com.waypolice.common.constant.BorrowConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 描述:
 *
 * @author zyp
 * @description 我的借阅service实现类
 * @create 2019-09-10 11:40
 */
@Service
public class MyBorrowServiceImpl implements MyBorrowService {

    Logger logger= LoggerFactory.getLogger(MyBorrowServiceImpl.class);

    @Autowired
    private ClusterCmsBorRegisterExtDao clusterCmsBorRegisterExtDao;

    @Autowired
    private ClusterCmsBorRegisterDao clusterCmsBorRegisterDao;

    @Autowired
    private ClusterCmsBrowseRegisterDao clusterCmsBrowseRegisterDao;

    /**
     * @author:zyp
     * @description 获取我的借阅内容
     * @date: 2019/9/10 14:37
     * @return 
     */
    @Override
    public ResultObjVO getMyBorrow(MyBorrowVo myBorrowVo) {
        Map<String,Object> parameterMap = new HashMap();
        parameterMap.put("userId",myBorrowVo.getUserId());
        List<MyBorrowVo> myBorrowVos = clusterCmsBorRegisterExtDao.getMyBorrow(parameterMap);
        return ResultUtil.success(myBorrowVos);
    }

    /**
     * @author:zyp
     * @description 归还线上已授权的文档
     * @date: 2019/9/10 15:23
     * @return
     */
    @Override
    public ResultObjVO returnAccreditArch(MyBorrowVo myBorrowVo) {
        List<Long> ids = myBorrowVo.getIds();
        if (ids==null||ids.size()==0) {
            return ResultUtil.error("请确认是否选中数据");
        }else{
            CmsBorRegister cmsBorRegister = new CmsBorRegister();
            cmsBorRegister.setBorStatus(3);
            CmsBorRegisterClusterExample cmsBorRegisterClusterExample = new CmsBorRegisterClusterExample();
            CmsBorRegisterClusterExample.Criteria criteria = cmsBorRegisterClusterExample.createCriteria();
            criteria.andIdIn(ids);
            clusterCmsBorRegisterDao.updateByExampleSelective(cmsBorRegister,cmsBorRegisterClusterExample);
            return ResultUtil.success();
        }
    }

    /**
     * @author:zyp
     * @description 查看线上文件
     * @date: 2019/9/10 15:59
     * @return 
     */
    @Override
    public byte[] watchOriginalImg(MyBorrowVo myBorrowVo) {
        CmsBrowseRegister cmsBrowseRegister = new CmsBrowseRegister();
        cmsBrowseRegister.setBrowseFileId(myBorrowVo.getFileId());
        int fileShape = 0;
        String pdfUrl = null;
        //如果当前的文件形式为卷、件 则 fileShape为1
        switch (myBorrowVo.getFileShape()){
            case "0":
            case "1":
                fileShape = 1;

                /**
                 * 待开发...根据用户id查询当前全宗号，在通过全宗号获取文件的http开头地址
                 */
                /**
                 * 待开发...根据文件的id查询查询文件的文件服务器的相对地址
                 */
//                CmsJudItem cmsJudItem = volAndItemService.selectByItemId(id);
//                String itemUrl = cmsJudItem.getItemStr2();
//                pdfUrl = itemUrl.replace(IMAGE_SERVER_URL,"");
                break;
            case "2":
                fileShape = (int)BorrowConstant.BORROW_FILE_SIN_ITEM;

                /**
                 * 待开发...根据用户id查询当前全宗号，在通过全宗号获取文件的http开头地址
                 */
                /**
                 * 待开发...根据文件的id查询查询文件的文件服务器的相对地址
                 */
//                CmsSinItem cmsSinItem = cmsSinItemService.selectSinItemById(id);
//                String sinUrl = cmsSinItem.getItemStr2();
//                pdfUrl = sinUrl.replace(IMAGE_SERVER_URL,"");
                break;
            default:
        }
        cmsBrowseRegister.setBrowseFileShape(fileShape);
        cmsBrowseRegister.setBrowseUserId((long)myBorrowVo.getUserId());
        clusterCmsBrowseRegisterDao.insertSelective(cmsBrowseRegister);

        byte[] bytedownloadFile = new byte[]{};
        try {
            FastDfsClient fastDFSClient = new FastDfsClient("classpath:client.conf");
            bytedownloadFile = fastDFSClient.getBytedownloadFile(pdfUrl);
        } catch (Exception e) {
            logger.error("连接fastDFS 失败！！");
        }
        return bytedownloadFile;
    }


}